package com.example.controller;


import com.example.config.OssConfig;
import com.example.entity.Information;
import com.example.entity.Operate;
import com.example.mapper.OperateMapper;
import com.example.service.IOperateService;
import com.example.util.Code;
import com.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2023-11-03
 */
@RestController
@RequestMapping("/s/operate")
@CrossOrigin
public class OperateController {
    @Autowired
    private IOperateService iOperateService;
    @Autowired
    private OccController controller;
    @Autowired
    private OperateMapper operateMapper;
    //添加运营信息
    @PostMapping("/add")
    public Result add(@RequestBody Operate operate){
        int add = iOperateService.add(operate);
        if(add>0){
            return new Result(Code.SAVE_OK,"注册成功");
        }else {
            return new Result(Code.SAVE_ERR,"注册失败");
        }
    }
    //查看公司是否认证
    @GetMapping("/find/{id}")
    public Result findCompany(@PathVariable Integer id){
        Operate company = iOperateService.findCompany(id);
        if(company==null){
            return new Result(Code.SAVE_ERR,"没有此公司");
        }else {
            return new Result(Code.GET_OK,company,"查询成功");
        }
    }

    /**
     * 根据id查运营信息
     * @param id
     * @return
     */

    @GetMapping("/finds/{id}")
    public Result findall(@PathVariable Integer id){
        Operate byIdOperate = iOperateService.findByIdOperate(id);
        return new Result(Code.SAVE_OK,byIdOperate,"成功");
    }


    @GetMapping("/insert/{zid}")
    public Result insert(@PathVariable Integer zid){
        int add = iOperateService.insert(zid);
        return new Result(Code.SAVE_OK,add,"添加成功");
    }


//    @GetMapping("/change/{zid}/{src}")
//    public Result changeImg(@PathVariable Integer zid,@PathVariable String src){
//        int i = iOperateService.updateImg(zid, src);
//        return new Result(Code.GET_OK,"修改成功");
//    }

    @PostMapping("/updateOp")
    public Result changeOp(@RequestBody Operate operate){
        int i = iOperateService.updateOp(operate);
        return new Result(Code.UPDATE_OK,i,"修改成功");
    }
    @PostMapping("/addzidcard/{zid}")
    public Result addIdzCard(@RequestParam MultipartFile file, @PathVariable Integer zid) throws IOException {
        Result result = controller.uploadImg(file);
        Operate operate = new Operate();
        operate.setDoorhead((String) result.getData());
        operate.setZid(zid);
        int i = operateMapper.updateImg(operate);
        return new Result(Code.GET_OK,i,"成功");
    }
    @GetMapping("/aaa/{zid}")
    public Result a2(@PathVariable Integer zid){
        return new Result(Code.SAVE_OK,operateMapper.findDoor(zid),"成功");
    }
}
